package com.startrek.ch02;

public class WhitespaceReplacement {

    // pos是字符数组中最后一个原来的有效字符
    public String replaceWhiteSpaces(char[] target, int pos) {
        if (target == null || target.length == 0)
            throw new IllegalArgumentException("Input string is null or empty");

        int end = target.length - 1;
        while (pos != end) {
            if (target[pos] != 32) {
                target[end--] = target[pos--];
            } else {
                target[end--] = '0';
                target[end--] = '2';
                target[end--] = '%';
                pos--;
            }
        }

        return new String(target);
    }
}
